/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen Zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **    Get the current and remote sites deployment name and role, and put them into the actionPlanOutputs.
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Get the current and remote sites deployment name and role, and put them into the actionPlanOutputs.
 * 
 * There are no parameters can be configured in the action.
 * But get site deployment name and role will be save into actionPlanOutput object to pass it next action to handle.
 * 
 * Action output:
 *   Local_Deployment_Name: local deployment site name.
 *   REMOTE_Deployment_Name: remote deployment site name.
 *   Local_Role: local deployment site role.
 *   Remote_Role: remote deployment site role.
 * 
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class GetDeploymentNameAndRoleActionHandler extends AbstractActionHandler {

    private static Logger logger = LoggerFactory.getLogger(GetDeploymentNameAndRoleActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {
        

        String currentDeploymentName = Utils.getConfigurationValue(Constants.CFG_GRL_DEPLOY_NAME);
        String remoteDeploymentName = Utils.getCorrespondSiteName(currentDeploymentName);
        String role = "";
        if (Utils.checkMasterSite(currentDeploymentName))
            role = Constants.ROLE_MASTER;
        else if (Utils.checkSlaveSite(currentDeploymentName))
            role = Constants.ROLE_SLAVE;

        String remoteRole = "";
        if (Utils.checkMasterSite(remoteDeploymentName))
            remoteRole = Constants.ROLE_MASTER;
        else if (Utils.checkSlaveSite(remoteDeploymentName))
            remoteRole = Constants.ROLE_SLAVE;

        if (logger.isDebugEnabled()) {
            logger.debug("Deployment name is: {}, role is:{}", new Object[]{currentDeploymentName, role});
        }
        actionPlanOutputs.put(Constants.STATUS_LOCAL_DEPLOYMENT_NAME, currentDeploymentName);
        actionPlanOutputs.put(Constants.STATUS_LOCAL_ROLE, role);
        actionPlanOutputs.put(Constants.STATUS_REMOTE_DEPLOYMENT_NAME, remoteDeploymentName);
        actionPlanOutputs.put(Constants.STATUS_REMOTE_ROLE, remoteRole);


    }

}
